package com.yixia.system.mapper.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yixia.common.core.domain.entity.SysRole;
import com.yixia.common.core.domain.entity.SysUser;
import com.yixia.common.utils.StringUtils;
import com.yixia.common.utils.page.PageBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 35038
*/

@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {


    default int updateUser(SysUser sysUser){
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        /*1、添加修改数据*/
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getDeptId()), SysUser::getDeptId, sysUser.getDeptId());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getUsername()), SysUser::getUsername, sysUser.getUsername());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getNickName()), SysUser::getNickName, sysUser.getNickName());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getEmail()), SysUser::getEmail, sysUser.getEmail());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getPhonenumber()), SysUser::getPhonenumber, sysUser.getPhonenumber());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getSex()), SysUser::getSex, sysUser.getSex());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getAvatar()), SysUser::getAvatar, sysUser.getAvatar());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getPassword()), SysUser::getPassword, sysUser.getPassword());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getStatus()), SysUser::getStatus, sysUser.getStatus());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getLoginIp()), SysUser::getLoginIp, sysUser.getLoginIp());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getLoginDate()), SysUser::getLoginDate, sysUser.getLoginDate());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getUpdateBy()), SysUser::getUpdateBy, sysUser.getUpdateBy());
        updateWrapper.set(StringUtils.isNotEmpty(sysUser.getRemark()), SysUser::getRemark, sysUser.getRemark());
        updateWrapper.eq(SysUser::getUserId, sysUser.getUserId());
        updateWrapper.eq(SysUser::getDelFlag, '0');
        /*2、在修改时myBatis-plus会自动修改updataById和updataTime*/
        /*3、进行修改操作*/
        return this.update(null, updateWrapper);
    }

    /**
     * 根据条件分页查询用户列表
     * @param user
     * @return
     */
    default List<SysUser> selectUserList(SysUser user){


        return null;
    }

    /**
     * 通过用户ID查询用户
     * @param userId 用户ID
     * @return 用户对象信息
     */
    default SysUser selectUserById(Long userId){
        return this.selectById(userId);
    }

    /**
     * 根据用户名查询用户信息
     * @param username 用户名
     * @return 查询的用户
     */
    default SysUser getByUsername(String username){
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username).eq(SysUser::getDelFlag, '0');
        return this.selectOne(queryWrapper);
    }


  /*  default Page<SysUser> selectUserPageList(SysUser sysUser){
        *//*1. 构建查询条件*//*
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getDelFlag, '0')
                .eq(StringUtils.isNotEmpty(sysUser.getUserId()), SysUser::getUserId, sysUser.getUserId())
                .eq(StringUtils.isNotEmpty(sysUser.getStatus()), SysUser::getStatus, sysUser.getStatus())
                .like(StringUtils.isNotEmpty(sysUser.getUsername()), SysUser::getUsername, sysUser.getUsername())
                .like(StringUtils.isNotEmpty(sysUser.getPhonenumber()), SysUser::getPhonenumber, sysUser.getPhonenumber());
        if(StringUtils.isNotEmpty(sysUser.getDeptId())){
            queryWrapper.apply(" (sys_user.dept_id = "+ sysUser.getDeptId() + " OR sys_user.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set("+ sysUser.getDeptId() + ", ancestors) ))");
        }

        *//*2. 进行时间的查询*//*
        queryWrapper.gt(StringUtils.isNotEmpty(sysUser.getBeginTime()), SysUser::getCreateTime, sysUser.getBeginTime())
                .lt(StringUtils.isNotEmpty(sysUser.getEndTime()),SysUser::getCreateTime, sysUser.getEndTime());

        *//*3. 指定查询顺序*//*
        queryWrapper.orderByAsc(SysUser::getUserId);

        *//*4. 构建分页查询条件*//*
        Page<SysUser> userPage = new Page<>();
        if(StringUtils.isNotNull(pageBean)){
            userPage = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        }

        *//*4. 进行查询*//*
        return this.selectPage(userPage, queryWrapper);
    }*/


    /**
     * 校验用户名称是否唯一
     *
     * @param username 用户名称
     * @return 结果
     */
    public SysUser checkUserNameUnique(String username);

    /**
     * 校验手机号码是否唯一
     *
     * @param phonenumber 手机号码
     * @return 结果
     */
    public SysUser checkPhoneUnique(String phonenumber);

    /**
     * 检验邮箱账号是否唯一
     * @param email 邮箱号
     * @return 结果
     */
    SysUser checkEmailUnique(String email);

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    default int deleteUserByIds(Long[] userIds){
        if(userIds.length > 0) {
            List<Long> collect = Arrays.stream(userIds).collect(Collectors.toList());
            LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(SysUser::getDelFlag, "2").in(SysUser::getUserId, collect);
            return this.update(null, updateWrapper);
        }
        return 0;
    }

    // 根据用户名查询用户信息
    default SysUser selectUserByUsername(String username){
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username).eq(SysUser::getDelFlag, '0');
        return this.selectOne(queryWrapper);
    }

    Page<SysUser> selectUserListPage(Page<SysUser> page, @Param("user") SysUser user);

    Page<SysUser> selectUnallocatedList(Page<SysUser> page, @Param("user") SysUser user);

    /**
     * 修改用户头像
     *
     * @param userName 用户名
     * @param avatar   头像地址
     * @return 结果
     */
    default int updateUserAvatar(String userName, String avatar){
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SysUser::getAvatar, avatar).eq(SysUser::getUsername, userName);
        SysUser sysUser = new SysUser();
        sysUser.setAvatar(avatar);
        return this.update(sysUser, updateWrapper);
    }

    // 进行分页查询
    List<SysUser> selectPageList(SysUser user);/*{
        // 查询条件的构造
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(user.getStatus()), SysUser::getStatus, user.getStatus())
                .eq(SysUser::getDelFlag, "0")
                .like(StringUtils.isNotEmpty(user.getUsername()), SysUser::getUsername, user.getUsername())
                .like(StringUtils.isNotEmpty(user.getPhonenumber()), SysUser::getPhonenumber, user.getPhonenumber());

        queryWrapper.gt(user.getBeginTime() != null, SysUser::getCreateTime, user.getBeginTime());
        queryWrapper.lt(user.getEndTime() != null,SysUser::getCreateTime, user.getEndTime());
        // 进行查询
        return this.selectList(queryWrapper);
    }*/

    List<SysUser> selectSenatorUserByRoleId(Long deptId, String ancestors);
}




